package com.powernode.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.powernode.domain.Prod;
import com.powernode.model.Result;
import com.powernode.service.ProdService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品业务管理控制层
 */
@RestController
@Api(tags = "商品业务管理控制层")
@RequestMapping("prod/prod")
public class ProdController {

    @Autowired
    private ProdService prodService;


    /**
     * 多条件分页查询商品
     *
     * @param current  当前页
     * @param size     每页显示的记录
     * @param prodName 商品名称
     * @param status   状态
     * @return
     */
    @ApiOperation("多条件分页查询商品")
    @GetMapping("page")
    @PreAuthorize("hasAuthority('prod:prod:page')")
    public Result<Page<Prod>> loadProdPage(@RequestParam Long current,
                                           @RequestParam Long size,
                                           @RequestParam(required = false) String prodName,
                                           @RequestParam(required = false) Long status) {
//        多条件分页查询商品
        Page<Prod> prodPage = new Page<>(current, size);
        prodPage = prodService.page(prodPage, new LambdaQueryWrapper<Prod>()
                .eq(ObjectUtil.isNotNull(status), Prod::getStatus, status)
                .like(StringUtils.hasText(prodName), Prod::getProdName, prodName)
                .orderByDesc(Prod::getCreateTime)
        );
        return Result.success(prodPage);
    }


    /**
     * 新增商品
     *
     * @param prod 商品对象
     * @return
     */
    @ApiOperation("新增商品")
    @PostMapping
    @PreAuthorize("hasAuthority('prod:prod:save')")
    public Result<String> saveProd(@RequestBody Prod prod) {
        Boolean saved = prodService.saveProd(prod);
        return Result.handle(saved);
    }


    /**
     * 根据标识查询商品详情
     *
     * @param prodId 商品标识
     * @return
     */
    @ApiOperation("根据标识查询商品详情")
    @GetMapping("info/{prodId}")
    @PreAuthorize("hasAuthority('prod:prod:info')")
    public Result<Prod> loadProdInfo(@PathVariable Long prodId) {
        Prod prod = prodService.queryProdInfoById(prodId);
        return Result.success(prod);
    }


    /**
     * 修改商品信息
     *
     * @param prod 商品对象
     * @return
     */
    @ApiOperation("修改商品信息")
    @PutMapping
    @PreAuthorize("hasAuthority('prod:prod:update')")
    public Result<String> modifyProdInfo(@RequestBody Prod prod) {
        Boolean modified = prodService.modifyProdInfo(prod);
        return Result.handle(modified);
    }


    /**
     * 根据标识删除商品
     *
     * @param prodId 商品标识
     * @return
     */
    @ApiOperation("根据标识删除商品")
    @DeleteMapping("{prodId}")
    @PreAuthorize("hasAuthority('prod:prod:delete')")
    public Result<String> removeProd(@PathVariable Long prodId) {
        Boolean removed = prodService.removeProdById(prodId);
        return Result.handle(removed);
    }


    ///////////////////////////////////feign接口////////////////////////////////////////
    @GetMapping("getProdListByIds")
    public Result<List<Prod>> getProdListByIds(@RequestParam List<Long> prodIdList) {
        return Result.success(prodService.listByIds(prodIdList));
    }

}
